package cn.com.winning.ecare.common;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import cn.com.winning.ecare.utils.StringUtil;

public class MyTimeUtil
{

	public static String yyyy = "yyyy";
	public static String yyyy_MM_dd = "yyyy-MM-dd";
	public static String yyyyMMdd = "yyyyMMdd";
	public static String yyyyMM = "yyyyMM";
	public static String yyyy_MM = "yyyy-MM";
	public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
	public static String yyyyMMddHHmm = "yyyyMMddHHmm";
	public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static String yyyy_MM_dd_HH_mm_ = "yyyy年MM月dd日 HH时mm分";
	public static String yyyy_MM_ = "yyyy年MM月";
	public static String yyyy_MM_dd_ = "yyyy年MM月dd日";

	public static String getCurrentTime()
	{
		Date now = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
		String str1 = simpleDateFormat.format(now);
		return str1;
	}

	/**
	 * 将字符串时间改成Date类型
	 * 
	 * @param format
	 * @param dateStr
	 * @return
	 */
	public static Date strToDate(String format, String dateStr)
	{

		Date date = null;

		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			date = simpleDateFormat.parse(dateStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 将Date时间转成字符串
	 * 
	 * @param format
	 * @param date
	 * @return
	 */
	public static String DateToStr(String format, Date date)
	{

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

		return simpleDateFormat.format(date);
	}

	/**
	 * 获取2个字符日期的天数差
	 * 
	 * @param p_startDate
	 * @param p_endDate
	 * @return 天数差
	 */
	public static long getDaysOfTowDiffDate(String p_startDate, String p_endDate)
	{

		Date l_startDate = MyTimeUtil.strToDate(MyTimeUtil.yyyy_MM_dd, p_startDate);
		Date l_endDate = MyTimeUtil.strToDate(MyTimeUtil.yyyy_MM_dd, p_endDate);
		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenDays = (long) ((l_endTime - l_startTime) / (1000 * 60 * 60 * 24));
		return betweenDays;
	}

	/**
	 * 获取2个Date型日期的分钟数差值
	 * 
	 * @param p_startDate
	 * @param p_endDate
	 * @return 分钟数差值
	 */
	public static long getMinutesOfTowDiffDate(Date p_startDate, Date p_endDate)
	{

		long l_startTime = p_startDate.getTime();
		long l_endTime = p_endDate.getTime();
		long betweenMinutes = (long) ((l_endTime - l_startTime) / (1000 * 60));
		return betweenMinutes;
	}

	/**
	 * 获取2个字符日期的天数差
	 * 
	 * @param p_startDate
	 * @param p_endDate
	 * @return 天数差
	 */
	public static long getDaysOfTowDiffDate(Date l_startDate, Date l_endDate)
	{

		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenDays = (long) ((l_endTime - l_startTime) / (1000 * 60 * 60 * 24));
		return betweenDays;
	}

	/**
	 * 给出日期添加一段时间后的日期
	 * 
	 * @param dateStr
	 * @param plus
	 * @return
	 */
	public static String getPlusDays(String format, String dateStr, long plus)
	{

		Date date = MyTimeUtil.strToDate(format, dateStr);

		long time = date.getTime() + plus * 24 * 60 * 60 * 1000;

		return MyTimeUtil.DateToStr(format, new Date(time));
	}

	/**
	 * 给出日期添加一段时间后的日期
	 * 
	 * @param dateStr
	 * @param plus
	 * @return
	 */
	public static String getPlusDays(String format, Date date, long plus)
	{

		long time = date.getTime() + plus * 24 * 60 * 60 * 1000;

		return MyTimeUtil.DateToStr(format, new Date(time));
	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 * @throws ParseException
	 */
	public static String getWeekOfDate(String dt)
	{
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		try
		{
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dt));
		}
		catch (ParseException e)
		{
			return "";
		}

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	/**
	 * 得到本月的第一天
	 * 
	 * @return
	 */
	public static String getMonthFirstDay()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return MyTimeUtil.DateToStr(yyyy_MM_dd, calendar.getTime());
	}

	/**
	 * 得到本月的最后一天
	 * 
	 * @return
	 */
	public static String getMonthLastDay()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return MyTimeUtil.DateToStr(yyyy_MM_dd, calendar.getTime());
	}

	public static String GetSysDate(String format, String StrDate, int year, int month, int day)
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sFmt = new SimpleDateFormat(format);
		cal.setTime(sFmt.parse((StrDate), new ParsePosition(0)));

		if (day != 0)
		{
			cal.add(cal.DATE, day);
		}
		if (month != 0)
		{
			cal.add(cal.MONTH, month);
		}
		if (year != 0)
		{
			cal.add(cal.YEAR, year);

		}
		return sFmt.format(cal.getTime());
	}

	public static Calendar getCalendar(String format, String dateStr)
	{
		Calendar calendar = Calendar.getInstance();
		if (StringUtil.isNotEmpty(dateStr))
		{
			calendar.setTime(strToDate(format, dateStr));
		}
		return calendar;
	}
}
